﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Emgu.CV.Structure;
using Emgu.CV;

namespace Domotica.Gazing
{
    public class Face
    {
        public Face(Rectangle rect)
        {
            Rectangle = rect;
            _Eyes = new EyeList();
        }

        /// <summary>
        /// Bounding rectangle for the object (average rectangle of a group)
        /// </summary>
        public Rectangle Rectangle { get; set; }

        public Rectangle CalculateRectangle()
        {
            return new Rectangle(Rectangle.X + Rectangle.Width / 3, 
                Rectangle.Y + Rectangle.Height / 3,
                Rectangle.Width / 3, Rectangle.Height / 3);
        }
        
        private readonly EyeList _Eyes;
        public EyeList Eyes
        {
            get
            {
                return _Eyes;
            }
        }

        private Eye _RightEye;
        private bool _RightEyeSearched = false;
        public Eye RightEye
        {
            get
            {
                if (!_RightEyeSearched)
                {
                    _RightEye = FindEye(EyePosition.Right);
                    _RightEyeSearched = true;
                }
                return _RightEye;
            }
        }

        private Eye _LeftEye;
        private bool _LeftEyeSearched = false;
        public Eye LeftEye
        {
            get
            {
                if (!_LeftEyeSearched)
                {
                    _LeftEye = FindEye(EyePosition.Left);
                    _LeftEyeSearched = true;
                }
                return _LeftEye;
            }
        }

        private Eye FindEye(EyePosition position)
        {
            Eye foundEye = null;
            int width = int.MaxValue;
            foreach (Eye eye in Eyes)
            {
                if (eye.Position == position
                    && eye.Rectangle.Width < width)
                {
                    width = eye.Rectangle.Width;
                    foundEye = eye;
                }
            }
            return foundEye;
        }

        #region "methods"

        public void Draw(Image<Bgr, Byte> image)
        {
            image.Draw(Rectangle, new Bgr(Color.Blue), 2);
        }

        public void DrawAllEyes(Image<Bgr, Byte> image)
        {
            foreach (Eye eye in Eyes)
            {
                eye.Draw(image);
            }
        }

        public void DrawEyes(Image<Bgr, Byte> image)
        {
            if (RightEye != null)
            {
                RightEye.Draw(image);
            }
            if (LeftEye != null)
            {
                LeftEye.Draw(image);
            }
        }

        #endregion

        public Image<Gray, byte> GetImage(Image<Gray, byte> image)
        {
            return image.Copy(Rectangle);
        }

        public Image<Bgr, byte> GetImage(Image<Bgr, byte> image)
        {
            return image.Copy(Rectangle);
        }
    }

    public class FaceList : List<Face>
    {
    }
}
